/*
 * @Author: your name
 * @Date: 2020-06-08 14:50:25
 * @LastEditTime: 2020-06-12 14:06:49
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \IM_Web\im_web\src\store\actions\index.js
 */
// import store from '../'
/* 导出actions方法 */
import { showLoading, hideLoading, showFullscreenImg, hideFullscreenImg } from './widgetUi'
import { initNimSDK } from './initNimSDK'
import { resetSearchResult, searchUsers, searchTeam } from './search'
import { deleteSession, setCurrSession, resetCurrSession } from './session'
import { markSysMsgRead, resetSysMsgs, deleteSysMsgs, markCustomSysMsgRead } from './sysMsgs'
import { sendMsg, sendFileMsg, sendMsgReceipt, sendRobotMsg, revocateMsg, updateLocalMsg, getHistoryMsgs, resetNoMoreHistoryMsgs, continueRobotMsg } from './msgs'
import { delegateTeamFunction, onTeamNotificationMsg, enterSettingPage, getTeamMembers, checkTeamMsgReceipt, getTeamMsgReads } from './team'


import router from '../../router';
import { Message } from 'iview'
// import SDK from '@/js/js/NIM_Web_NIM_v7.6.0.js'

function connectNim({ state, commit, dispatch }, obj) {
    console.log(commit)
    let { force } = Object.assign({}, obj)
    console.log(!state.nim)
    // 操作为内容页刷新页面，此时无nim实例
    if (!state.nim || force) {
        // console.log(sessionStorage.getItem('info'))
        let loginInfo = {
            uid: sessionStorage.getItem('info') == null || sessionStorage.getItem('info') === "" ? "" : JSON.parse(sessionStorage.getItem('info')).accid,
            sdktoken: sessionStorage.getItem('info') == null || sessionStorage.getItem('info') === "" ? "" : JSON.parse(sessionStorage.getItem('info')).token
        }
        if (!loginInfo.uid) {
            // 无cookie，直接跳转登录页
            // pageUtil.turnPage('无历史登录记录，请重新登录', 'login')
            if (router.app._route.name === 'login' || router.app._route.name === "passport") {
                return
            }
            Message.error('无历史登录记录，请重新登录')
            // console.log('当前路由=>', router.app._route.name);
            router.push('/login')
            // console.log('无历史登录记录，请重新登录')
        } else {
            console.log('loginInfo',loginInfo)
            // 有cookie，重新登录
            dispatch('initNimSDK', loginInfo)
        }
    }
}


export default {
    // 连接sdk请求，false表示强制重连
    connect(store, obj) {
        let { type } = Object.assign({}, obj)
        // type 可为 nim chatroom
        type = type || 'nim'
        switch (type) {
            case 'nim':
                connectNim(store, obj)
                break
        }
    },
    // 用户触发的登出逻辑
    logout({ state, commit }) {
        sessionStorage.clear();
        commit('updateUserUID', { uid: '', sdktoken: '' })
        if (state.nim) {
            state.nim.disconnect()
        }
        commit("resetNim")
        router.push('/login')
    },
    initNimSDK,
    resetSearchResult,
    searchUsers,
    updateRefreshState({ commit }) {
        commit('updateRefreshState')
    },
    // UI 及页面状态变更
    continueRobotMsg,
    showLoading,
    hideLoading,
    showFullscreenImg,
    hideFullscreenImg,

    // 标记系统消息已读
    markSysMsgRead,
    markCustomSysMsgRead,
    resetSysMsgs,
    deleteSysMsgs,

    // 发送消息
    sendMsg,
    sendFileMsg,
    sendRobotMsg,
    // 发送消息已读回执
    sendMsgReceipt,
    // 消息撤回
    revocateMsg,
    // 更新本地消息
    updateLocalMsg,
    getHistoryMsgs,
    // 重置历史消息状态
    resetNoMoreHistoryMsgs,

    // 删除会话
    deleteSession,
    // 设置当前会话
    setCurrSession,
    // 重置当前会话
    resetCurrSession,

    // 搜索群
    searchTeam,
    // 代理sdk中的群方法
    delegateTeamFunction,
    // 处理群消息回调
    onTeamNotificationMsg,
    // 进入群信息设置页
    enterSettingPage,
    // 获取群成员
    getTeamMembers,
    // 群消息回执检查
    checkTeamMsgReceipt,
    // 查询群消息回执已读列表
    getTeamMsgReads,


    aEditNim(context, val) {
        context.commit('editNim', val)
    },
    aEditTbas(context, val) {
        context.commit('editTbas', val)
    },
}